﻿using Api;
using Api.util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace OcmKeep
{
    static class Program
    {
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);
        private const int SW_RESTORE = 9;
        private const int SW_SHOWNORMAL = 1;
        private static void HandleRunningInstance(Process instance)
        {
            IntPtr hWnd = instance.MainWindowHandle;
            if (IsIconic(hWnd))
                ShowWindowAsync(hWnd, SW_RESTORE);
            ShowWindowAsync(hWnd, SW_SHOWNORMAL);//显示  
            SetForegroundWindow(hWnd);//当到最前端  
            ShowWindow(hWnd, SW_SHOWNORMAL);
        }

        private static Process RuningInstance()
        {
            Process currentProcess = Process.GetCurrentProcess();
            Process[] Processes = Process.GetProcessesByName("闸机控制");
            foreach (Process process in Processes)
            {
                if (process.Id != currentProcess.Id)
                {
                    return process;
                }
            }
            return null;
        }  
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常   
                Application.ThreadException += Application_ThreadException;
                //处理非UI线程异常   
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                Thread.Sleep(500);
                Process form = RuningInstance();
                if (form != null)
                {
                    HandleRunningInstance(form);
                }
                else
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(typeof(Exception),"程序重启！！ 异常："+ex.Message+"\r\n 堆栈："+ ex.StackTrace);
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ee = e.ExceptionObject as Exception;
            LogHelper.WriteLog(typeof(Exception), "未捕获异常 程序重启！！ 异常：" + ee.Message + "\r\n 堆栈：" + ee.StackTrace);
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Exception ee = e.Exception;
            LogHelper.WriteLog(typeof(Exception), "未捕获异常 程序重启！！ 异常：" + ee.Message + "\r\n 堆栈：" + ee.StackTrace);
            //Application.Restart();
        }
    }
}
